/*
 * Copyright (C) 2016 MarkMjw
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hashcoding.eyes.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.hashcoding.eyes.R;
import com.hashcoding.eyes.data.constant.APIConstant;
import com.hashcoding.eyes.data.entity.Photo;
import com.hashcoding.eyes.data.entity.PhotosList;
import com.hashcoding.eyes.module.utils.Util;
import com.hashcoding.eyes.ui.adapter.BaseListAdapter;
import com.hashcoding.eyes.ui.adapter.PhotosListAdapter;
import com.hashcoding.eyes.ui.protocol.IScrollListener;
import com.hashcoding.eyes.ui.view.EmptyLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 搜索页面
 *
 * @author markmjw
 * @date 2016-01-23
 */
public class SearchActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    @Bind(R.id.search_edit)
    EditText mSearchEdit;
    @Bind(R.id.list_view)
    ListView mListView;
    @Bind(R.id.error_layout)
    EmptyLayout mEmptyLayout;

    private PhotosListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        initView();
    }

    @OnClick(R.id.title_cancel)
    protected void onTitleRightClick() {
        onBackPressed();
    }

    private void initView() {
        mEmptyLayout.setState(EmptyLayout.STATE_HIDE);

        mAdapter = new PhotosListAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        mListView.setOnScrollListener(new IScrollListener() {
            @Override
            public void onLoadMore() {
                if (mAdapter.canLoadMore()) {
                    mAdapter.setState(BaseListAdapter.STATE_LOADING);
                    doSearch("", true);
                }
            }
        });

        mSearchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH
                        || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    if (!search()) {
                        return true;
                    }
                }
                return false;
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        PhotoActivity.launch(this, mAdapter.getItem(position));
    }

    private boolean search() {
        Editable editable = mSearchEdit.getText();
        if (null != editable && editable.length() > 0) {
            String searchText = editable.toString().trim();
            if (!TextUtils.isEmpty(searchText)) {
                mSearchEdit.clearFocus();
                Util.hideSoftInput(mSearchEdit);
                doSearch(searchText, false);
                return true;
            } else {
                mSearchEdit.setText("");
            }
        }
        return false;
    }

    private void doSearch(String text, final boolean loadMore) {
        if (!loadMore) {
            mEmptyLayout.setState(EmptyLayout.STATE_LOADING);
            mAdapter.setState(BaseListAdapter.STATE_LESS_ONE_PAGE);
        }
        mSearchEdit.postDelayed(new Runnable() {
            @Override
            public void run() {
                List<Photo> photos = PhotosList.instance().getList();
                if (loadMore) {
                    mAdapter.addList(photos);
                    mAdapter.setState(photos.size() > APIConstant.PAGE_COUNT
                            ? BaseListAdapter.STATE_LOAD_MORE
                            : BaseListAdapter.STATE_LESS_ONE_PAGE);
                } else if (photos.size() == 0) {
                    mAdapter.setList(photos);
                    mEmptyLayout.setState(EmptyLayout.STATE_EMPTY);
                    mAdapter.setState(BaseListAdapter.STATE_NO_DATA);
                } else {
                    mAdapter.setList(photos);
                    mEmptyLayout.setState(EmptyLayout.STATE_HIDE);
                    mAdapter.setState(photos.size() >= APIConstant.PAGE_COUNT
                            ? BaseListAdapter.STATE_LOAD_MORE
                            : BaseListAdapter.STATE_NO_MORE);
                }
            }
        }, 1000);
    }
}
